﻿/* Write a program that reverses the words in given sentence.
	Example: "C# is not C++, not PHP and not Delphi!"  "Delphi not and PHP, not C++ not is C#!".
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace _13.ReverseWords
{
    class ReverseWords
    {
        static void Main()
        {
            Console.Write("Enter a string you want to be reversed: ");
            string str = Console.ReadLine();
            //string str = "C# is not C++, not PHP and not Delphi!";

            MatchCollection words = Regex.Matches(str, @"\b(?<word>[\w|\#\+]+)");
            MatchCollection signs = Regex.Matches(str, @"(,|!|\.|\?)");
            int mBound = words.Count;
            List<string> newSentence = new List<string>();
            int sign = 0;
            for (int i = 0; i < mBound; i++)
            {
                newSentence.Add(words[mBound - i - 1].ToString());
                if (signs.Count > sign && String.Join(" ", newSentence).Length >= signs[sign].Index)
                {
                    newSentence[newSentence.Count - 1] += signs[sign].ToString();
                    sign++;
                }
            }
            Console.WriteLine(String.Join(" ", newSentence));

            /* ne raboti - trqbva da se dobavi i length-a na dumata 
            string sentence_with_reversed_words = null;

            char[] punctuationmarks = { '!', ',', '.', '?' };
            List<int> punctuationpositions = new List<int>();
            List<char> punctuationmarks_ = new List<char>();
            for (int i = 0; i < str.Length; i++)
            {
                foreach (char ch in punctuationmarks)
                {
                    if (ch == str[i])
                    {
                        punctuationpositions.Add(i);
                        punctuationmarks_.Add(str[i]);
                    }
                }
            }


            string str_tempstack = str;
            foreach (char ch in punctuationmarks)
            {
                str_tempstack = str_tempstack.Replace(ch.ToString(), "");
            }
            
            string[] arrofwords = str_tempstack.Split(' ');
            for (int i = arrofwords.Length - 1; i >= 0; i--)
            {
                sentence_with_reversed_words += " " + arrofwords[i];
            }

            string newstr = sentence_with_reversed_words;
            int pi = 0;
            for (int i = 0; i < sentence_with_reversed_words.Length; i++)
            {
                newstr += sentence_with_reversed_words[i];
                Console.WriteLine("l: " + newstr.Length);
                foreach (int p in punctuationpositions)
                {
                    Console.WriteLine(p);
                    if (i == p)
                    {
                        newstr += punctuationmarks_[pi];
                        pi++;
                    }
                }
            }
            Console.WriteLine(newstr);
             */
        }
    }
}
